package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.EventTag;
import com.tim.app.ui.adapter.ActionAdapter;


/**
 * 弹窗
 */
public class ActionPop extends PopupWindow implements ActionAdapter.OnActionListener, View.OnClickListener {

    private Context mContext;
    private ListView lvAction;
    private Button btn_cancel;
    private ActionAdapter actionAdapter;
    private String[] actionArray;

    private int deletePositon;

    public ActionPop(Context context,int positon) {
        this.mContext = context;
        setWidth(RT.getScreenWidth() * 10 / 13);
        setHeight(-2);
        this.deletePositon = positon;
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        setAnimationStyle(R.style.ReportPopAnim);
        View view = LayoutInflater.from(context).inflate(R.layout.popup_action, null);
        setContentView(view);
        initView(view);
        initData();
    }

    private void initView(View view) {
        lvAction = (ListView) view.findViewById(R.id.lvAction);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
    }

    private void initData() {
        actionArray = mContext.getResources().getStringArray(R.array.actionArray);
        actionAdapter = new ActionAdapter(actionArray);
        actionAdapter.setOnReportListener(this);
        lvAction.setAdapter(actionAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    @Override
    public void onAction(int position) {
        EventManager.ins().sendEvent(EventTag.DO_DELETE, 0, 0, deletePositon);
        dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
